#ifndef _THREAD_H_
#define _THREAD_H_

#include <iostream>
#include <string>
#include <pthread.h>
#include <unistd.h>
#include <cstdio>
#include <cstring>
#include <vector>
#include <functional>
#include <stdlib.h>

namespace ThreadModule
{
    static uint32_t number = 0;

    template <typename T>
    class Thread
    {
        using func_t = std::function<void(T)>;

    public:
        Thread(func_t func, T data)
            : _tid(0), _isdetach(false), _isrunning(false), _res(nullptr), _func(func), _data(data)
        {
            _name = "thread-" + std::to_string(number++);
        }
        ~Thread()
        {
        }

        void Detach()
        {
            if (_isdetach)
                return;
            if (_isrunning) // 启动之后手动设置分离
                pthread_detach(_tid);
            EnableDetach(); // 启动之前分离, 改标志位
        }

        bool Start()
        {
            if (_isrunning)
                return false;
            int n = pthread_create(&_tid, nullptr, Routine, this);
            if (n != 0)
            {
                std::cerr << "create thread error" << strerror(n) << std::endl;
                return false;
            }
            else
            {
                std::cout << _name << " create success" << std::endl;
                return true;
            }
        }

        bool Stop()
        {
            if (_isrunning)
            {
                int n = pthread_cancel(_tid);
                if (n != 0)
                {
                    std::cerr << "cancel thread error" << strerror(n) << std::endl;
                    return false;
                }
            }
            else
            {
                _isrunning = false;
                std::cout << _name << "stop success" << std::endl;
                return true;
            }
            return false;
        }

        void Join()
        {
            if (_isdetach)
            {
                std::cout << "你的线程已经是分离的了, 不能join了" << std::endl;
                return;
            }
            int n = pthread_join(_tid, &_res);
            if (n != 0)
            {
                std::cerr << "join thread error" << strerror(n) << std::endl;
            }
            else
            {
                std::cout << "join thread success" << std::endl;
            }
        }

    private:
        void EnableDetach()
        {
            std::cout << "线程被分离了" << std::endl;
            _isdetach = true;
        }
        void EnableRunning()
        {
            _isrunning = true;
        }
        // 不带static会变成类内函数, 默认隐藏参数this, 创建导致格式不兼容
        static void *Routine(void *args) // 传入当前对象地址
        {
            Thread<T> *self = static_cast<Thread<T> *>(args);
            self->EnableRunning(); // 启动
            if (self->_isdetach)
                self->Detach();
            self->_func(self->_data); // 回调处理
            return nullptr;
        }

    private:
        pthread_t _tid;
        std::string _name;
        bool _isdetach;
        bool _isrunning;
        void *_res;
        func_t _func;
        T _data;
    };

} // namespace ThreadModule

#endif